package com.yanqu.road.dao.impl.activity.stronghold;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdEliminateUnionScore;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdEliminateUnionScoreDaoImpl extends TempDataObjectDao<StrongholdEliminateUnionScore> {
    public static String getStrongholdEliminateUnionScoreSql = "select * from t_c_activity_stronghold_eliminate_union_score where activityId=?";

    public Map<String, StrongholdEliminateUnionScore> getStrongholdEliminateUnionScore(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, StrongholdEliminateUnionScore> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getStrongholdEliminateUnionScoreSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdEliminateUnionScore strongholdEliminateUnionScore = parse(rs);
                dataMap.put(strongholdEliminateUnionScore.getUnionId(), strongholdEliminateUnionScore);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateStrongholdEliminateUnionScoreSql = "update t_c_activity_stronghold_eliminate_union_score set historyScore=?,curRoundScore=?,resetScoreRound=?,unionBaseInfo = ? where activityId=? and unionId=?";

    @Override
    protected boolean updateData(StrongholdEliminateUnionScore strongholdEliminateUnionScore) {
        strongholdEliminateUnionScore.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateUnionScore.getHistoryScore().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateUnionScore.getCurRoundScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdEliminateUnionScore.getResetScoreRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdEliminateUnionScore.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdEliminateUnionScore.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateUnionScore.getUnionId()));
        if (getDb().executeNoneQuery(updateStrongholdEliminateUnionScoreSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdEliminateUnionScoreSql = "insert into t_c_activity_stronghold_eliminate_union_score(activityId,unionId,historyScore,curRoundScore,resetScoreRound,unionBaseInfo) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(StrongholdEliminateUnionScore strongholdEliminateUnionScore) {
        strongholdEliminateUnionScore.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdEliminateUnionScore.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateUnionScore.getUnionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateUnionScore.getHistoryScore().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateUnionScore.getCurRoundScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdEliminateUnionScore.getResetScoreRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdEliminateUnionScore.getUnionBaseInfo())));
        if (getDb().executeNoneQuery(insertStrongholdEliminateUnionScoreSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdEliminateUnionScore parseData(ResultSet rs) throws SQLException {
        StrongholdEliminateUnionScore strongholdEliminateUnionScore = new StrongholdEliminateUnionScore();
        strongholdEliminateUnionScore.setActivityId(rs.getInt("activityId"));
        strongholdEliminateUnionScore.setUnionId(rs.getString("unionId"));
        strongholdEliminateUnionScore.setHistoryScore(new BigInteger(rs.getString("historyScore")));
        strongholdEliminateUnionScore.setCurRoundScore(new BigInteger(rs.getString("curRoundScore")));
        strongholdEliminateUnionScore.setResetScoreRound(rs.getInt("resetScoreRound"));
        strongholdEliminateUnionScore.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));
        return strongholdEliminateUnionScore;
    }

}
